#!/bin/bash

export GIT_REF=`git show --pretty=format:%h --quiet`
export GIT_BRANCH=${TRAVIS_BRANCH:-`git rev-parse --abbrev-ref HEAD | tr -d '\n'`}

export TZ=":Europe/London" # UTC
export GIT_ISO_DATE=`git show --pretty=format:%cd --date=iso --quiet`
export PKG_DATE=`ruby -rdate -e 'puts Time.at(DateTime.parse(ENV["GIT_ISO_DATE"]).to_time.to_i).strftime("%Y_%m_%d.%H_%M")'`

set -e
set -v

ls -lah dist/* || true

cd dist

for filename in ./*.{dmg,deb,rpm,snap,exe,zip,AppImage,pacman,apk}; do
  [ -e "$filename" ] || continue
  echo "Uploading $filename to https://postbird.paxa.kuber.host/$PKG_DATE-$GIT_BRANCH-$GIT_REF ..."
  curl "https://postbird.paxa.kuber.host/$PKG_DATE-$GIT_BRANCH-$GIT_REF/$filename" \
    -T "$filename" \
    -H "Upload-Token: $UPLOAD_TOKEN" \
    --retry 3 --retry-delay 10 --http1.1
done
